import Toast from '../vant/weapp/toast/toast';

const http = (url, param, method, callback) => {
    // param.admin_id = wx.getStorageSync('admin_id')
    wx.request({
        url: url,
        header: {
            token: wx.getStorageSync('token'),
            header: {
                'content-type': 'application/x-wwww-form-urlencoded;charset=utf-8',
            },
            // UserAuthKey: '6fe819df-2901-45fa-a764-db359791ab16268',
        },
        data: param,
        method: method,
        success: (res) => {
            if (res.data.code == 200 || res.data.code == 2000 || res.data.code == 0 || res.data.code == 1002 ) {
                callback(res.data);
                // console.log(res, 99)
                Toast.clear()
            } else {
                if (res.data.code == 401) {
                    wx.setStorageSync('token', '')
                    wx.setStorageSync('mobile', '')
                    // Toast({
                    //     type: 'fail',
                    //     message: '请先登录！',
                    //     forbidClick: true,
                    //     onClose: () => {
                    //         // wx.reLaunch({
                    //         //     url: '/pages/person/index',
                    //         // })
                            
                    //     },
                    // });
                    callback(res.data);
                    return;

                }
                if(url=="https://apiqixuke.qishenghui.net/order/delete"){
                    //防止弹窗
                    Toast.clear()
                    callback(res.data);
                    return
                }
                Toast({
                    type: 'fail',
                    forbidClick: true,
                    message: res.data.message,
                });
                callback(res.data);
            }
        },
        fail: (res) => {
            console.log(res)
            Toast({
                type: 'fail',
                message: '系统忙不过来了,请稍后重试',
            });
            callback(res);
        }
    });
}

export function get(url, param, callback) {
    return http(url, param, 'get', callback);
}

export function post(url, param, callback) {
    return http(url, param, 'post', callback);
}

const wecatpay = (order_on, type, callback) => {
    wx.login({
        success(codes) {
            if (codes.code) {
                post("https://apiqixuke.qishenghui.net/Wecat/index", {
                    order_on: order_on,
                    code: codes.code,
                    type: type
                }, (re) => {
                    console.log(re)
                    wx.requestPayment({
                        appid: re.data.appId,
                        timeStamp: re.data.timeStamp,
                        nonceStr: re.data.nonceStr,
                        package: re.data.package,
                        signType: re.data.signType,
                        paySign: re.data.paySign,
                        success(da) {
                            if (da.errMsg == 'requestPayment:ok') {
                                Toast({
                                    type: 'success',
                                    message: '购买成功',
                                    onClose: () => {

                                    },
                                });

                            }
                            callback(da);
                        },
                        fail() {
                            Toast({
                                type: 'fail',
                                message: '购买失败',
                                onClose: () => {

                                },
                            });
                            callback();
                        }
                    })
                })

            }
        }
    })

}
const wecatpayfz = (order_on, type, callback) => {
    wx.login({
        success(codes) {
            if (codes.code) {
                post("https://apiqixuke.qishenghui.net/Wecat/indexv1", {
                    order_on: order_on,
                    code: codes.code,
                    type: type
                }, (re) => {
                    console.log(re)
                    wx.requestPayment({
                        appid: re.data.appId,
                        timeStamp: re.data.timeStamp,
                        nonceStr: re.data.nonceStr,
                        package: re.data.package,
                        signType: re.data.signType,
                        paySign: re.data.paySign,
                        success(da) {
                            console.log(da)
                            if (da.errMsg == 'requestPayment:ok') {
                                
                                Toast({
                                    type: 'success',
                                    message: '购买成功',
                                    onClose: () => {

                                    },
                                });

                            }
                          
                            callback(da);
                        },
                        fail() {
                            console.log('fks')
                            Toast({
                                type: 'fail',
                                message: '购买失败',
                                onClose: () => {

                                },
                            });
                            callback();
                        }
                    })
                })

            }
        }
    })

}
export function wecat(order_on, type, callback) {
    return wecatpay(order_on, type, callback);
}

export function wecatfz(order_on, type, callback) {
    return wecatpayfz(order_on, type, callback);
}